import 'package:flutter_demo/constants/http_response_code.dart';
import 'package:flutter_demo/ui/graphQL_test/model/base_response.dart';
import 'http_error.dart';

///
/// @Author sundy
/// @Date 2024/1/9 17:19
///
/// @Description 网络请求基础结构
class HttpBaseResponse<T> extends BaseResponse {
  bool isRespListData = false;

  ///响应数据
  T? data;

  ///响应List数据对象(该字段只对客户端使用，用于将data为list结构时，将序列化的list对象存放在该字段上)
  List<T>? dataList;

  ///请求结果RawJson
  Map<String, dynamic>? json;

  HttpBaseResponse(
      {code = HttpResponseCode.defaultHttpCode, message, this.data})
      : super(code: code, message: message);

  ///是否请求成功
  get isSuccess =>
      code == HttpResponseCode.success ||
      code == HttpResponseCode.deviceUnExist;

  ///获取响应数据
  T? getResponseDate() {
    if (data != null) {
      return data!;
    } else {
      return null;
    }
  }

  ///生成网络请求错误Model
  HttpError buildHttpError() => HttpError(code: code, message: message);
}
